package ru.linnenson.darshana.infrastructure.persistence.repositories;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import ru.linnenson.darshana.infrastructure.persistence.models.QPhysicTaskStore;
import ru.linnenson.darshana.infrastructure.rest.dto.PhysicTasksCriteria;

@Component
public class PhysicTaskPredicateBuilder {

  public Predicate build(final PhysicTasksCriteria criteria) {

    BooleanBuilder expression = new BooleanBuilder();

    if (!CollectionUtils.isEmpty(criteria.getSources())) {
      expression.and(QPhysicTaskStore.physicTaskStore.source.id.in(criteria.getSources()));
    }

    if (!CollectionUtils.isEmpty(criteria.getLevels())) {
      expression.and(QPhysicTaskStore.physicTaskStore.level.id.in(criteria.getLevels()));
    }

    if (!CollectionUtils.isEmpty(criteria.getNumbersInKim())) {
      expression.and(QPhysicTaskStore.physicTaskStore.numberInKim.id.in(criteria.getNumbersInKim()));
    }

    if (!CollectionUtils.isEmpty(criteria.getThemes())) {
      expression.and(QPhysicTaskStore.physicTaskStore.theme.id.in(criteria.getThemes()));
    }

    if (criteria.isWithSolution()) {
      expression.and(QPhysicTaskStore.physicTaskStore.solutionPhotoIds.isNotEmpty());
    }

    if (StringUtils.hasText(criteria.getText())) {
      expression.and(QPhysicTaskStore.physicTaskStore.text.contains(criteria.getText()));
    }

//    if (CollectionUtils.isEmpty(criteria.getTags())) {
//      expression.and(QPhysicTaskStore.physicTaskStore.tags.);
//    }

    return expression;
  }
}
